package com.ivr.cm.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ivr.cm.dao.IConfigFilterDao;



import com.ivr.cm.util.BaseUtil;
import com.ivr.cm.vo.ConfigFilterVo;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * 过滤配置信息数据实现
 * @author liugeng
 * @date 2015-12-15
 * @version 1.0
 *
 */
public class ConfigFilterDaoImpl extends JdbcDaoSupport implements IConfigFilterDao {
	private static Logger logger = Logger.getLogger(ConfigFilterDaoImpl.class);
	/**获得所有的过滤配置信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ConfigFilterVo> getConfigFilterVoList() {
		String sql = "select filter.id, filter.num, filter.sp_number, filter.province, filter.start_time, filter.end_time, filter.thres_hold, ifnull(count_group.bill_count, 0) thres_index from " +
				"( SELECT bill.sp_number, COUNT(bill.mobile) bill_count FROM tb_ivr_user_bill as bill WHERE bill.add_date BETWEEN CURDATE() AND NOW() GROUP BY bill.sp_number ) as count_group " +
				"right join tb_ivr_config_filter as filter on filter.sp_number = count_group.sp_number " + 
				"where filter.status = 1 order by id";
		logger.info(String.format("获得所有的过滤配置信息列表[%s]", BaseUtil.logSQL(sql, null)));
		List<ConfigFilterVo> configFilterVoList = this.getJdbcTemplate().query(sql,rowMapper);
		if(configFilterVoList != null && configFilterVoList.size() > 0){
			return configFilterVoList;
		}
		return null;
	}
	
	private ParameterizedRowMapper<ConfigFilterVo> rowMapper = new ParameterizedRowMapper<ConfigFilterVo>() {
		@Override
		public ConfigFilterVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ConfigFilterVo configFilterVo = new ConfigFilterVo();
			configFilterVo.setId(rs.getInt("id"));
			configFilterVo.setNum(rs.getInt("num"));
			configFilterVo.setThres_index(rs.getInt("thres_index"));
			configFilterVo.setSp_number(rs.getString("sp_number"));
			configFilterVo.setProvince(rs.getString("province"));
			configFilterVo.setStart_time(rs.getString("start_time"));
			configFilterVo.setEnd_time(rs.getString("end_time"));
			String tr_thres_hold = rs.getString("thres_hold");
			if((tr_thres_hold == null) || (configFilterVo.getNum() != tr_thres_hold.split(",").length)) {
				createThres_hold(configFilterVo);
			}
			else {
				String[] temp_thres = tr_thres_hold.split(",");
				List<Integer> thres_list = new ArrayList<Integer>();
				for(int i = 0; i < temp_thres.length; i++) {
					thres_list.add(Integer.parseInt(temp_thres[i]));
				}
				configFilterVo.setThres_hold(thres_list);
			}
			return configFilterVo;
		}
	};
	
	private int createThres_hold(ConfigFilterVo configFilterVo) {
		
		List<Integer> thres_hold = configFilterVo.getThres_hold();
		thres_hold.clear();
		
		if(configFilterVo.getNum() == 0)
			return 0;
		
		for(int i = 0; i < configFilterVo.getNum() ; i++) {
			int random = (int) (1+Math.random() * 100);
			logger.info(String.format("长号码[%s],随机数量[%d]，产生随机数序列号为[%d]", configFilterVo.getSp_number(), configFilterVo.getNum(), random));
			thres_hold.add(random);
		}
		
		configFilterVo.setThres_index(0);
		
		return thres_hold.size();
	}
}
